package com.ljx.Array;

import com.ljx.LeetCode;

/**
 * @author 李加喜
 * @Package com.ljx.Array
 * @date 2021-11-22 20:34
 */
// 304 二维区域和检索
public class ShuZu {
    private int[][] preNum;
    public static void main(String[] args) {
        int[][]num={{3,0,1,4,2},{5,6,3,2,1},{1,2,0,1,5},{4,1,0,1,7},{1,0,3,0,5}};
        ShuZu shuZu = new ShuZu(num);
        System.out.println(shuZu);
    }

    /**
     * 只是用于存储数字进行使用
     */
    public ShuZu(int[][] matrix) {
        int length = matrix.length;
        int n= matrix[0].length;
        if (length==0 || n==0){
            return;
        }
        //preNum[i][j]计算的是  因为扩大了一层 所以1,1就是之前的0.0
        preNum=new int[length+1][n+1];
        for (int i=1;i<=length;i++){
            for (int j=1;j<=n;j++){
                // 计算[0,0,i,j]的和
                preNum[i][j]=preNum[i-1][j]+preNum[i][j-1]+matrix[i-1][j-1]-preNum[i-1][j-1];
            }
        }
        System.out.println("1");
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        return preNum[row2+1][col2+1]-preNum[row1][col2+1]
                -preNum[row2+1][col1]+preNum[row1][col1];
    }
}
